#! /usr/bin/env python
# -*- coding: utf-8 -*-

import random

from Tetrominoes import Tetrominoes

coordsTable = (
                ((0, 0), (0, 0), (0, 0), (0, 0)),
                ((-1, 1), (0, 1), (-1, 0), (0, 0)),
                ((0, 1), (-1, 0), (0, 0), (-1, -1)),
                ((-1, 1), (-1, 0), (0, 0), (0, -1)),
                ((-1, 1), (-1, 0), (0, 0), (-1, -1)),
                ((-1, 2), (-1, 1), (-1, 0), (0, 0)),
                ((0, 2), (0, 1), (0, 0), (-1, 0)),
                ((0, 2), (0, 1), (0, 0), (0, -1))
              )


class JcShape(object):
    def __init__(self):
        self.coords = [[0, 0] for i in range(4)]
        self.pieceShape = Tetrominoes.NoShape
        self.setShape(Tetrominoes.NoShape)

    def shape(self):
        return self.pieceShape

    def setShape(self, shape):
        table = coordsTable[shape]
        for i in range(4):
            for j in range(2):
                self.coords[i][j] = table[i][j]
        self.pieceShape = shape

    def setRandomShape(self):
        self.setShape(random.randint(1, 7))

    def x(self, index):
        return self.coords[index][0]

    def y(self, index):
        return self.coords[index][1]

    def setX(self, index, x):
        self.coords[index][0] = x

    def setY(self, index, y):
        self.coords[index][1] = y

    def minX(self):
        _min = self.coords[0][0]
        for i in range(4):
            _min = min(_min, self.coords[i][0])
        return _min

    def maxX(self):
        _max = self.coords[0][0]
        for i in range(4):
            _max = max(_max, self.coords[i][0])
        return _max

    def minY(self):
        _min = self.coords[0][1]
        for i in range(4):
            _min = min(_min, self.coords[i][1])
        return _min

    def maxY(self):
        _max = self.coords[0][1]
        for i in range(4):
            _max = max(_max, self.coords[i][1])
        return _max

    def rotated_left(self):
        if self.pieceShape == Tetrominoes.SquareShape:
            return self
        else:
            result = JcShape()
            result.pieceShape = self.pieceShape
            for i in range(4):
                result.setX(i, self.y(i))
                result.setY(i, -self.x(i))
            return result

    def rotated_right(self):
        if self.pieceShape == Tetrominoes.SquareShape:
            return self
        else:
            result = JcShape()
            result.pieceShape = self.pieceShape
            for i in range(4):
                result.setX(i, -self.y(i))
                result.setY(i, self.x(i))
            return result

if __name__ == '__main__':
    shape = JcShape()
    shape.setShape(Tetrominoes.ZShape)
    print(shape.coords)
    shape = shape.rotate_left()
    print(shape.coords)

    shape.setShape(Tetrominoes.ZShape)
    print(shape.coords)
    shape = shape.rotate_right()
    print(shape.coords)